package com.fira.edu.service.impl;

import java.util.List;
import com.fira.common.utils.DateUtils;
import com.fira.edu.domain.EduStudent;
import com.fira.edu.domain.dto.parent.ParentStudentsDTO;
import com.fira.edu.domain.vo.ParentStudentsVO;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.mapper.EduStudentMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fira.edu.mapper.EduParentMapper;
import com.fira.edu.domain.EduParent;
import com.fira.edu.service.IEduParentService;
import com.fira.common.core.text.Convert;

/**
 * 家长信息Service业务层处理
 * 
 * @author wuzhenggui
 * @date 2021-10-11
 */
@Service
public class EduParentServiceImpl implements IEduParentService 
{
    @Autowired
    private EduParentMapper eduParentMapper;
    @Autowired
    private EduStudentMapper eduStudentMapper;

    /**
     * 查询家长信息
     * 
     * @param id 家长信息ID
     * @return 家长信息
     */
    @Override
    public EduParent selectEduParentById(Long id)
    {
        return eduParentMapper.selectEduParentById(id);
    }

    /**
     * 查询家长信息列表
     * 
     * @param eduParent 家长信息
     * @return 家长信息
     */
    @Override
    public List<EduParent> selectEduParentList(EduParent eduParent)
    {
        return eduParentMapper.selectEduParentList(eduParent);
    }

    @Override
    public List<EduParent> selectEduParentListByPhones(List<String> phones) {
        return eduParentMapper.selectEduParentListByPhones(phones);
    }

    /**
     * 新增家长信息
     * 
     * @param eduParent 家长信息
     * @return 结果
     */
    @Override
    public int insertEduParent(EduParent eduParent)
    {
        eduParent.setId(IdUtils.fastSimpleUUID());
        eduParent.setCreateTime(DateUtils.getNowDate());
        return eduParentMapper.insertEduParent(eduParent);
    }

    /**
     * 修改家长信息
     * 
     * @param eduParent 家长信息
     * @return 结果
     */
    @Override
    public int updateEduParent(EduParent eduParent)
    {
        eduParent.setUpdateTime(DateUtils.getNowDate());
        return eduParentMapper.updateEduParent(eduParent);
    }

    /**
     * 删除家长信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEduParentByIds(String ids)
    {
        return eduParentMapper.deleteEduParentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除家长信息信息
     * 
     * @param id 家长信息ID
     * @return 结果
     */
    @Override
    public int deleteEduParentById(Long id)
    {
        return eduParentMapper.deleteEduParentById(id);
    }

    /**
     * 小程序家长中心-查询家长在读的学生列表
     *
     * @param parentStudentsDTO
     * @return
     */
    @Override
    public List<ParentStudentsVO> selectParentStudents(ParentStudentsDTO parentStudentsDTO) {
        List<ParentStudentsVO> voList = eduParentMapper.selectParentStudents(parentStudentsDTO);
        if(CollectionUtils.isNotEmpty(voList)){
            voList.forEach(item -> {
                EduStudent student = eduStudentMapper.selectEduStudentById(item.getStudentId());
                try {
                    if(student.getStudentBirthday() != null)
                        item.setStudentAge(DateUtils.getAge(student.getStudentBirthday()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        return eduParentMapper.selectParentStudents(parentStudentsDTO);
    }
}
